﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MapInfo.Data;
using MapInfo.Mapping;
using MapInfo.Engine;
using MapInfo.Geometry;

namespace MapXtremeMapFormApplication1
{
    
    public partial class MapForm1 : Form
    {
        private string FileName;
        public MapForm1()
        {
            InitializeComponent();
            this.mapToolBarButtonOpenTable.ImageIndex = 39;
            mapControl1.Map.ViewChangedEvent += new ViewChangedEventHandler(Map_ViewChangedEvent);
            Map_ViewChangedEvent(this, null);
            this.listBox1.Items.Add("请加载地图");
        }

        void Map_ViewChangedEvent(object sender, ViewChangedEventArgs e)
        {
            // Display the zoom level
            Double dblZoom = Convert.ToDouble(String.Format("{0:E2}", mapControl1.Map.Zoom.Value));
            if (statusStrip1.Items.Count > 0)
            {
                statusStrip1.Items[0].Text = "缩放: " + dblZoom.ToString() + " " + CoordSys.DistanceUnitAbbreviation(mapControl1.Map.Zoom.Unit);
            }
        }

        private void MapForm1_Load(object sender, EventArgs e)
        {

        }

        private void mapControl1_MouseMove(object sender, MouseEventArgs e)
        {
            PointF DisplayPoint = new PointF(e.X, e.Y);
            DPoint MapPoint = new MapInfo.Geometry.DPoint();
            DisplayTransform converter = this.mapControl1.Map.DisplayTransform;
            converter.FromDisplay(DisplayPoint, out MapPoint);

            toolStripStatusLabel1.Text ="DisplayPoint: " + e.X.ToString() + ", " + e.Y.ToString() + " Cursor Location: " + MapPoint.x.ToString() + ", " + MapPoint.y.ToString();
        }

        private void mapControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if(this.mapToolBarButtonSelect.Pushed)
                mapControl1_MouseDoubleClick(sender, e);
        }

        private void mapControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //MapInfo.Data.Catalog _catalog = MapInfo.Engine.Session.Current.Catalog;
            if (true == mapControl1.Map.Empty)
            {
                MessageBox.Show("请加载地图");
                return;
            }
            PointF DisplayPoint = new PointF(e.X, e.Y);
            DPoint MapPoint = new MapInfo.Geometry.DPoint();

            DisplayTransform converter = this.mapControl1.Map.DisplayTransform;
            converter.FromDisplay(DisplayPoint, out MapPoint);
            try
            {
                Map _map = mapControl1.Map;
                Catalog _catalog = Session.Current.Catalog;
                Cursor.Current = Cursors.WaitCursor;

                //设置查找半径
                Distance d = MapInfo.Mapping.SearchInfoFactory.ScreenToMapDistance(_map, 3);
                SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchNearest(MapPoint, _map.GetDisplayCoordSys(), d);
                IResultSetFeatureCollection irfc = _catalog.Search(FileName, si);//WORLD是地图名，可以改成变量

                Session.Current.Selections.DefaultSelection.Clear();
                Session.Current.Selections.DefaultSelection.Add(irfc);

                this.listBox1.Items.Clear();
                foreach (Feature f in irfc)
                {
                    if (f.ToString() != "")
                    {
                        this.listBox1.Items.Add("key:\t\t" + f.Key.ToString());
                        this.listBox1.Items.Add("BaseTable:\t" + irfc.BaseTable.Alias);
                        this.listBox1.Items.Add("Table:\t\t" + f.Table);
                        this.listBox1.Items.Add("Feature:\t" + f.ToString());
                        this.listBox1.Items.Add("FileName:\t" + FileName);
                    }
                }
                irfc.Clear();
             }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void mapToolBar1_ButtonClick(object sender, EventArgs e)
        {
            if (this.mapToolBarButtonOpenTable.Pushed)
            {
                DoMapfromFile();
                if (false == mapControl1.Map.Empty)
                {
                    this.mapToolBarButtonSelect.Enabled = true;
                    this.mapToolBarButtonSelectRect.Enabled = true;
                    this.mapToolBarButtonSelectRadius.Enabled = true;
                    this.mapToolBarButtonPan.Enabled = true;
                    this.mapToolBarButtonZoomIn.Enabled = true;
                    this.mapToolBarButtonZoomOut.Enabled = true;
                    this.mapToolBarButtonSelect.Pushed = true;
                }
                this.mapToolBarButtonOpenTable.Pushed = false;
            }
        }

        private void DoMapfromFile()
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Multiselect = true;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.DefaultExt = "TAB;GST;MWS";
            openFileDialog1.Filter =
                "全部地图文件(*.tab)(*.gst)(*.mws)|*.tab;*.gst;*.mws|" +
                "MapInfo Tables (*.tab)|*.tab|" +
                "MapInfo Geoset (*.gst)|*.gst|" +
                "MapInfo WorkSpace (*.mws)|*.mws|" +
                "全部 (*)|*";

            string strCantOpenList = null;
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                foreach (string filename in openFileDialog1.FileNames)
                {
                    try
                    {
                        Map map = Session.Current.MapFactory.CreateFromFile(filename, System.IntPtr.Zero, mapControl1.Size);
                        mapControl1.Map = map;
                        
                        FileName = filename.Substring(filename.LastIndexOf('\\') + 1, filename.LastIndexOf('.') - filename.LastIndexOf('\\') - 1);
                    }
                    catch (MapException me)
                    {
                        if (strCantOpenList == null)
                        {
                            strCantOpenList = me.Arg;
                        }
                        else
                        {
                            strCantOpenList = strCantOpenList + ", " + me.Arg;
                        }
                    }
                }
            }
            if (strCantOpenList != null)
            {
                MessageBox.Show("The following failed to open: " + strCantOpenList);
            }
        }
    }
}